/*
 * Result.java
 *
 * Created on October 28, 2009, 5:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author tungtt_b00546
 */
public class Result extends BaseDAO {

    /** Creates a new instance of Result */
    Connection con = getConnection();

    public Result() {
    }

    public boolean InsertResult(int testID, String UserName, String StartTime, String EndTime, int AnsertTrue, float Mark, int status) {
        try {
            PreparedStatement ps = con.prepareStatement("Insert into Result values (?,?,?,?,?,?,?)");
            ps.setInt(1, testID);
            ps.setString(2, UserName);
            ps.setString(3, StartTime);
            ps.setString(4, EndTime);
            ps.setInt(5, AnsertTrue);
            ps.setFloat(6, Mark);
            ps.setInt(7, status);
            if (ps.executeUpdate() == 1) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

//    public static void main(String[] agrs) {
//        Result r = new Result();
//        r.getConnection();
//
//        if (r.InsertResult(1, "phongtn", "01/01/1990", "01/01/1990", 2,5, 1) == true) {
//            System.out.println("Thanh cong ");
//        } else {
//            System.out.println("Thanh Thanh cong ");
//        }
//
//    }
    public ResultSet selectResult() {
        ResultSet rs = null;
        try {
            Statement s = con.createStatement();
            rs = s.executeQuery("Select * from Result where status = 1 ");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    public void insertDetail(int ID, String[] quesID, int AnswerID) throws SQLException {
        PreparedStatement preSQL;
        PreparedStatement preSQL2;
        String sql = "INSERT INTO ResultDetail(ResultID,AnswerID) VALUES(?,?)";
        String sqlQuestion = "INSERT INTO ResultDetail(ResultID,QuestionID,AnswerID) VALUES(?,?,?)";



        for (int k = 0; k < quesID.length; k++) {
            int questionid = Integer.parseInt(quesID[k]);
            preSQL = con.prepareStatement(sqlQuestion);
            preSQL.setInt(1, ID);
            preSQL.setInt(2,questionid);
            preSQL.setInt(3,AnswerID);
            preSQL.executeUpdate();
        }
    }

    public ResultSet selectQuestion(int id){
        ResultSet rs =null;
        try{
            Statement s = con.createStatement();
            rs = s.executeQuery("select Distinct QuestionID from ResultDetail where ResultID = " + id);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return rs;
    }
    public ResultSet selectQuestionContent(int id){
        ResultSet rs =null;
        try{
            Statement s = con.createStatement();
            rs = s.executeQuery("select * from Question where QuestionID = " + id);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return rs;
    }
     public ResultSet SelectAnswerUser(int id){
        ResultSet rs =null;
        try{
            Statement s = con.createStatement();
            rs = s.executeQuery("select Distinct AnswerID from ResultDetail where ResultID = " + id);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return rs;
    }
      public ResultSet selectAnswerAdmin(int id){
        ResultSet rs =null;
        try{
            Statement s = con.createStatement();
            rs = s.executeQuery("select AnswerID from Answers where AnswerID =" + id);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return rs;
    }
      public ResultSet selectResultUser(String UserName){
        ResultSet rs =null;
        try{
            Statement s = con.createStatement();
            rs = s.executeQuery("select * from Result where UserName = "  +"'"+ UserName+"'");
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return rs;
    }

//    public static void main(String[] args) throws SQLException {
//        new Result().selectResultUser("tungtt");
//    }
}
